﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public partial class DeptDocForm : BSE.Windows.Test.BaseForm
    {
        public DeptDocForm()
        {
            InitializeComponent();
        }

        public override void RefreshForm()
        {
            ReCreateDeptTree();
        }

        private void ReCreateDeptTree()
        {
            try
            {
                List<Keshi> ks = AnesthesiaBLL.GetAllKeshi();

                treeView1.Nodes.Clear();
                TreeNode rootNode = treeView1.Nodes.Add("医院");
                rootNode.ImageIndex = 0;
                foreach (Keshi k in ks)
                {
                    TreeNode node = rootNode.Nodes.Add(k.KeshiID, k.KeshiName, 1);
                    node.Tag = k;
                }
                treeView1.ExpandAll();
                treeView1.SelectedNode = rootNode;
            }
            catch (Exception e)
            {
                MessageBox.Show("加载科室数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FillYishiList(List<Yishi> ys)
        {
            try
            {
                listView1.Items.Clear();
                foreach (Yishi y in ys)
                {
                    ListViewItem lv = listView1.Items.Add(y.YishiCode + " " + y.YishiName, 0);
                    lv.Tag = y;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("加载医师数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;

            try
            {
                List<Yishi> ys = AnesthesiaBLL.GetYishiByKeshi(treeView1.SelectedNode.Tag as Keshi);
                FillYishiList(ys);
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载医师数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void addKeshiItem_Click(object sender, EventArgs e)
        {
            KeshiForm form = new KeshiForm();
            if (DialogResult.OK == form.ShowDialog())
            {
                Keshi keshi = new Keshi();
                keshi.KeshiName = form.keshi.KeshiName;
                keshi.KeshiCode = form.keshi.KeshiCode;

                try
                {
                    AnesthesiaBLL.AddKeshi(keshi);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加科室失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                ReCreateDeptTree();
            }
        }

        private void editKeshiItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node == null || !(node.Tag is Keshi))
            {
                MessageBox.Show("请先选择科室！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }


            Keshi k = node.Tag as Keshi;
            KeshiForm form = new KeshiForm();
            form.keshi = k;

            if (DialogResult.OK == form.ShowDialog())
            {
                try
                {
                    AnesthesiaBLL.ModifyKeshi(form.keshi);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改科室失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                ReCreateDeptTree();
            }
        }

        private void delKeshiItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node == null || !(node.Tag is Keshi))
            {
                MessageBox.Show("请先选择科室！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }


            Keshi k = node.Tag as Keshi;

            if (MessageBox.Show("科室删除后将不能再使用。是否确定要删除？", "医院", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    AnesthesiaBLL.DeleteKeshi(k);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除科室失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                ReCreateDeptTree();
            }
        }

        private void toolStripButtonAddDoc_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node == null || !(node.Tag is Keshi))
            {
                MessageBox.Show("添加医生前请先选择科室！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            Keshi k = node.Tag as Keshi;

            YishiForm form = new YishiForm();
            if (DialogResult.OK == form.ShowDialog())
            {
                Yishi yishi = form.yishi;
                yishi.YishiKeshiID = k.KeshiID;

                try
                {
                    AnesthesiaBLL.AddYishi(yishi);
                    List<Yishi> ys = AnesthesiaBLL.GetYishiByKeshi(k);
                    FillYishiList(ys);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加医师失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButtonEditDoc_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择医生！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (listView1.SelectedItems.Count > 1)
            {
                MessageBox.Show("一次只能修改一位医生的信息！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            Yishi y = listView1.SelectedItems[0].Tag as Yishi;
            YishiForm form = new YishiForm();
            form.yishi = y;
            if (DialogResult.OK == form.ShowDialog())
            {
                Yishi yishi = form.yishi;

                try
                {
                    AnesthesiaBLL.ModifyYishi(yishi);
                    Keshi k = null;
                    if (treeView1.SelectedNode != null)
                        k = treeView1.SelectedNode.Tag as Keshi;
                    List<Yishi> ys = AnesthesiaBLL.GetYishiByKeshi(k);
                    FillYishiList(ys);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("修改医师失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void toolStripButtonDelDoc_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择医生！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            if (MessageBox.Show("医师删除后将不能再使用。是否确定要删除？", "医院", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    for (int i = 0; i < listView1.SelectedItems.Count; i++)
                    {
                        Yishi y = listView1.SelectedItems[i].Tag as Yishi;
                        AnesthesiaBLL.DeleteYishi(y);
                    }

                    Keshi k = null;
                    if (treeView1.SelectedNode != null)
                        k = treeView1.SelectedNode.Tag as Keshi;
                    List<Yishi> ys = AnesthesiaBLL.GetYishiByKeshi(k);
                    FillYishiList(ys);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除医师失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                
            }
        }
    }
}
